package pl.nazaweb.imedproplus.rest;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import pl.nazaweb.imedproplus.business.service.OrganizationStructureService;
import pl.nazaweb.imedproplus.rest.pojo.OrganizationStructurePojo;
import pl.nazaweb.imedproplus.rest.pojo.ResponsePojo;

@Stateless
@Path("/json/organizationstructure")
public class OrganizationStructure extends AbstractRest<OrganizationStructurePojo> {

    @EJB
    private OrganizationStructureService organizationStructureService;

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public ResponsePojo getOrganizationStructureList() {
        return this.getEntityPojoList();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/create")
    public ResponsePojo createOrganizationStructures(List<OrganizationStructurePojo> organizationStructurePojos) {
        return this.createEntities(organizationStructurePojos);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/destroy")
    public ResponsePojo removeOrganizationStructures(List<OrganizationStructurePojo> organizationStrucutrePojos) {
        return this.destroyEntities(organizationStrucutrePojos);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/update")
    public ResponsePojo updateOrganizationStructures(List<OrganizationStructurePojo> organizationStructurePojos) {
        return this.updateEntities(organizationStructurePojos);
    }

    @Override
    public List<OrganizationStructurePojo> getEntityPojoListResult() {
        List<OrganizationStructurePojo> result = new ArrayList<OrganizationStructurePojo>();
        for (pl.nazaweb.imedproplus.model.entity.OrganizationStructure org : this.organizationStructureService.getOrganizationStructureList()) {
            result.add(new OrganizationStructurePojo(org));
        }
        return result;
    }

    @Override
    public void persistNewEnities(List<OrganizationStructurePojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.OrganizationStructure> newEntities = new ArrayList<pl.nazaweb.imedproplus.model.entity.OrganizationStructure>();
        for (OrganizationStructurePojo pojo : pojos) {
            newEntities.add(pojo.getEntity());
        }
        this.organizationStructureService.addNewOrgaanizationStructures(newEntities);
    }

    @Override
    public void removeEnities(List<OrganizationStructurePojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.OrganizationStructure> entitiesToRemove = new ArrayList<pl.nazaweb.imedproplus.model.entity.OrganizationStructure>();
        for (OrganizationStructurePojo pojo : pojos) {
            entitiesToRemove.add(pojo.getEntity());
        }
        this.organizationStructureService.removeOrgaanizationStructures(entitiesToRemove);
    }

    @Override
    public void updateEnities(List<OrganizationStructurePojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.OrganizationStructure> entitiesToUpdate = new ArrayList<pl.nazaweb.imedproplus.model.entity.OrganizationStructure>();
        for (OrganizationStructurePojo pojo : pojos) {
            entitiesToUpdate.add(pojo.getEntity());
        }
        this.organizationStructureService.updateOrganizationStructures(entitiesToUpdate);
    }
}
